<?php
@session_start();

include_once("../lib/functions.php");
getProjectRootPath();
//die("DOC-ROOT: ".$_SERVER['DOCUMENT_ROOT']);
require_once($_SERVER['DOCUMENT_ROOT']."/updates/PostActions.php");
require_once($_SERVER['DOCUMENT_ROOT']."/updates/ReadNewsFeed.php");
//include_once ("../updates/PostActions.php");



try {
    $postAction = new PostAction();
    $loggedInId = 0;
    $actionCommand="";
    try {
        //try to read all psted values
        if (isset($_SESSION["Allyfolio_User_ID"]) && !empty($_SESSION["Allyfolio_User_ID"])) {
            $loggedInId = trim($_SESSION["Allyfolio_User_ID"]);
        }
        if (isset($_REQUEST["action"]) && !empty($_REQUEST["action"])) {
            $actionCommand = trim($_REQUEST["action"]);
        }
        
        switch($actionCommand){
            case "st_update":
                //check and update privacy settings
                if (isset($_POST["nPrivSet"]) && !empty($_POST["nPrivSet"])) {
                    $pclusterid=0;
                    if (isset($_REQUEST["pSetClusterId"]) && !empty($_REQUEST["pSetClusterId"])) {
                        $pclusterid = trim($_REQUEST["pSetClusterId"]);
                    }
                    if (is_numeric(trim($_POST["nPrivSet"])) && trim($_POST["nPrivSet"]) > 0) {
                        //update privacy setting only if setting is changed
                        if (trim($_POST["isModifyPrivSetting"]) == "true" ) {
                            //isModifyPrivSetting
                            //update user privacy settings
                            $PerformPostAction = new PerformPostAction();
                            $actionRes = $PerformPostAction->UpdatePrivacySettings($loggedInId, trim($_POST["nPrivSet"]),"Default_Setting",$pclusterid);
                            switch ($actionRes["err_code"]) {
                                case $postAction->Success["err_code"]:
                                    //update user privacy settings session
                                    $_SESSION['user_settings']['privacy_settings'] = trim($_POST["nPrivSet"]);
                                    die(responseWithErrorCode($actionRes["err_code"],$actionRes["msg"]));
                                    break;
                                case $postAction->Failure["err_code"]:
                                    die(responseWithErrorCode($actionRes["err_code"],$actionRes["msg"]));
                                    break;
                            }
                        }
                    }
                }
                break;
            case "st_updatefeedprivacy":
            //{feedid:postid,privacytype:privacytypeid,clusterid:clusteridsel,allowedFriends:allowedfrnds,ignoredFriends:ignfrnds}
                $feedid=0;
                $privacytype=0;
                $clusterid=0;
                $allowedFriends="";
                $ignoredFriends="";
                //check and update privacy settings
                if (isset($_POST["feedid"]) && !empty($_POST["feedid"])) {
                    if (is_numeric(trim($_POST["feedid"])) && trim($_POST["feedid"]) > 0) {
                        $feedid=trim($_POST["feedid"]);
                        $privacytype=trim($_POST["privacytype"]);
                        $clusterid=trim($_POST["clusterid"]);
                        $allowedFriends=trim($_POST["allowedFriends"]);
                        $ignoredFriends=trim($_POST["ignoredFriends"]);
                        
                        //update privacy setting only if setting is changed
                        if (trim($privacytype)>0 ) {
                            //isModifyPrivSetting
                            //update user privacy settings
                            $PerformPostAction = new PerformPostAction();
                            $actionRes = $PerformPostAction->updateFeedPrivacytype($loggedInId,$feedid,$privacytype,$clusterid,$allowedFriends,$ignoredFriends);
                            switch ($actionRes["err_code"]) {
                                case $postAction->Success["err_code"]:
                                    die(responseWithErrorCode($actionRes["err_code"],$actionRes["msg"]));
                                    break;
                                case $postAction->Failure["err_code"]:
                                    die(responseWithErrorCode($actionRes["err_code"],$actionRes["msg"]));
                                    break;
                            }
                        }
                    }
                }
                break;
        }
        

    }
    catch (exception $ex) {
        die('update_data.php: validating posted parameters():: \t' . $ex->getMessage() .
            "\n");
    }

    include_once($_SERVER['DOCUMENT_ROOT']."/ajax/post_script_src.php");
    

}
catch (Execption $ex) {
    die('Caught exception: ' . $e->getMessage() . "\n");
}

?>